/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;

import cn.webrx.config.SpringConfig;
import cn.webrx.entity.Users;
import cn.webrx.mapper.DbMapper;
import cn.webrx.mapper.RolesMapper;
import cn.webrx.mapper.StudentMapper;
import cn.webrx.mapper.UsersMapper;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-07-23 10:43:04
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class Demo {

    @Autowired
    private SqlSessionFactory sf;

    @Autowired
    private DbMapper dm;

    @Autowired
    private StudentMapper sm;

    @Autowired
    private UsersMapper um;

    @Autowired
    private RolesMapper rm;

    @Test
    public void t5() {
        var session = sf.openSession();
        var sm = session.getMapper(StudentMapper.class);
        System.out.println(sm.page());
        System.out.println(sm.page());
        System.out.println(sm.page());
        System.out.println(sm.page());

        System.out.println("---------------------------");
        System.out.println(sm.queryById(5));
        session.commit();//将当前会话的查询，保存到二级缓存中，
        System.out.println(sm.queryById(5));
        System.out.println(sm.queryById(5));
        System.out.println(sm.queryById(5));

        System.out.println("----------------------");
        var s2 = sf.openSession();
        var sm2 = s2.getMapper(StudentMapper.class);
        System.out.println(sm2.queryById(5));
    }

    @Test
    public void t4() {
        // System.out.println(rm.queryByName("删除"));
        //System.out.println(rm.queryByName("登录"));

        rm.queryAll().forEach(e -> System.out.println(e.getName() + "(" + e.getUsers().size() + ")"));

    }

    @Test
    public void t3() {
        Users u = um.queryByName("root");
        System.out.println(u);
        System.out.println(um.queryAll());
    }

    @Test
    public void t2() {
        int currpage = 1;
        int pagesize = 3;
        PageHelper.startPage(currpage, pagesize);
        System.out.println(sm.page());
    }

    @Test
    public void t1() {
        System.out.println(dm.dbInfo());
    }
}
